package com.h3c.nem.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.h3c.nem.dto.TbSchoolDTO;
import com.h3c.nem.dto.TbSchoolImportDTO;
import com.h3c.nem.mapper.TbSchoolMapper;
import com.h3c.nem.entity.TbSchool;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ITbSchoolService;
import com.h3c.nem.utils.AppBusinessException;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 实现类
 *
 * @author chenyifan
 * @since 2022-06-20 09:11:04
 */
@Service
public class TbSchoolServiceImpl extends ServiceImpl<TbSchoolMapper, TbSchool> implements ITbSchoolService {

    @Autowired
    TbSchoolMapper schoolMapper;

    @Autowired
    CommService commService;

    @Override
    public List<TbSchoolDTO> selectFuzzyByDto(TbSchoolDTO dto) {
        List<TbSchool> tbSchoolList = schoolMapper.selectList(new QueryWrapper<TbSchool>().lambda()
                .like(TbSchool::getSchoolName, dto.getSchoolName()));
        List<TbSchoolDTO> schoolList = tbSchoolList.stream().map(tbSchool -> {
            TbSchoolDTO tbSchoolDTO = new TbSchoolDTO();
            BeanUtils.copyProperties(tbSchool, tbSchoolDTO);
            tbSchoolDTO.setSchoolName(StringEscapeUtils.unescapeHtml4(tbSchool.getSchoolName()) );
            return tbSchoolDTO;
        }).collect(Collectors.toList());
        return schoolList;
    }

    @Override
    public Integer insertDto(TbSchoolDTO dto) {
        QueryWrapper<TbSchool> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_name", dto.getSchoolName());
        TbSchool school = schoolMapper.selectOne(queryWrapper);
        if (school != null){
            throw new AppBusinessException("院校信息已存在，请重新输入");
        }
        school = new TbSchool();
        BeanUtils.copyProperties(dto, school);
        school.setOperateTime(new Date());
        school.setOperatorInfo(commService.getLoginUserNameAndId());
        return schoolMapper.insert(school);
    }

    @Override
    public Integer updateDto(TbSchoolDTO dto) {
        TbSchool school = new TbSchool();
        BeanUtils.copyProperties(dto, school);
        school.setOperateTime(new Date());
        school.setOperatorInfo(commService.getLoginUserNameAndId());
        return schoolMapper.updateById(school);
    }

    @Override
    public Integer deleteDto(TbSchoolDTO dto) {
        return schoolMapper.deleteById(dto.getSchoolId());
    }

    @Override
    public Integer importUserNew(MultipartFile file) {
        String fileContent = commService.parseExcelByTemplate("SCHOOL_IMPORT", file);
        JSONArray jsonArray = JSONArray.parseArray(fileContent);
        Integer num = 0;
        for (Object jsonObj : jsonArray) {
            JSONObject jsonObject = JSONObject.parseObject(jsonObj.toString());
            TbSchoolImportDTO schoolImportDTO = JSONObject.parseObject(jsonObject.toJSONString(), TbSchoolImportDTO.class);
            if (!StringUtils.hasText(schoolImportDTO.getSchoolName()) || !StringUtils.hasText(schoolImportDTO.getOverseaFlag())){
                continue;
            }
            TbSchoolDTO tbSchoolDTO = new TbSchoolDTO();
            BeanUtils.copyProperties(schoolImportDTO, tbSchoolDTO);
            tbSchoolDTO.setOverseaFlag((schoolImportDTO.getOverseaFlag().equals("是")) ? 1 : 2);
            tbSchoolDTO.setFlag985((schoolImportDTO.getFlag985().equals("是")) ? 1 : 2);
            tbSchoolDTO.setFlag211((schoolImportDTO.getFlag211().equals("是")) ? 1 : 2);
            tbSchoolDTO.setFlagSyl((schoolImportDTO.getFlagSyl().equals("是")) ? 1 : 2);
            QueryWrapper<TbSchool> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("school_name", tbSchoolDTO.getSchoolName());
            TbSchool school = schoolMapper.selectOne(queryWrapper);
            if (school != null){
                continue;
            }
            school = new TbSchool();
            BeanUtils.copyProperties(tbSchoolDTO, school);
            school.setOperateTime(new Date());
            school.setOperatorInfo(commService.getLoginUserNameAndId());
            num = num + schoolMapper.insert(school);
        }
        return num;
    }

    @Override
    public File download(TbSchoolDTO dto, String templateCode, String fileName) {
        List<TbSchoolDTO> schoolDTOS = selectFuzzyByDto(dto);
        List<TbSchoolImportDTO> schoolImportDTOS = new ArrayList<>();
        for (TbSchoolDTO schoolDTO : schoolDTOS) {
            TbSchoolImportDTO schoolImportDTO = new TbSchoolImportDTO();
            BeanUtils.copyProperties(schoolDTO, schoolImportDTO);
            schoolImportDTO.setOverseaFlag(schoolDTO.getOverseaFlag() == 1 ? "是" : "否");
            schoolImportDTO.setFlag985(schoolDTO.getFlag985() == 1 ? "是" : "否");
            schoolImportDTO.setFlag211(schoolDTO.getFlag211() == 1 ? "是" : "否");
            schoolImportDTO.setFlagSyl(schoolDTO.getFlagSyl() == 1 ? "是" : "否");
            schoolImportDTOS.add(schoolImportDTO);
        }
        return commService.generateExportFile(fileName, templateCode, (List)schoolImportDTOS);
    }
}

